package com.sgzn.sgpay.persistence.manager;

import com.sgzn.sgpay.enums.SgsResponseCode;
import com.sgzn.sgpay.exception.SgpayException;
import com.sgzn.sgpay.persistence.dao.TShopMapper;
import com.sgzn.sgpay.persistence.domain.TShop;
import com.sgzn.sgpay.persistence.domain.TShopExample;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author huangsenguang
 * @version Id: ShopDaoManager.java, v 0.1 2018/5/19 10:57 huangsenguang Exp $$
 */
@Slf4j
@Component
public class ShopDaoManager {

    @Resource
    private TShopMapper tShopMapper;

    /**
     * 根据门店id集合查询门店列表信息
     * @param shopIdList
     * @return
     */
    public List<TShop> getTShopList(List<String> shopIdList) {
        if (shopIdList == null || shopIdList.size() == 0) {
            return new ArrayList<TShop>(0);
        }
        TShopExample ShopExample = new TShopExample();
        TShopExample.Criteria cra = ShopExample.createCriteria();
        cra.andShopIdIn(shopIdList);
        return tShopMapper.selectByExample(ShopExample);
    }

    /**
     * 根据门店id 查询单个门店信息
     * @param shopId
     * @return
     */
    public TShop getTShop(String shopId) {
        TShop tShop = null;
        TShopExample tShopExample = new TShopExample();
        tShopExample.createCriteria().andShopIdEqualTo(shopId);
        List<TShop> tShops = tShopMapper.selectByExample(tShopExample);
        if (tShops == null || tShops.size() == 0) {
            throw new SgpayException(SgsResponseCode.C_SHOP_NOT_FOUND.getResponseCode(),SgsResponseCode.C_SHOP_NOT_FOUND.getResponseDesc());
        }
        tShop = tShops.get(0);
        return tShop;
    }

    /**
     * 根据商户编号， 查询所有属于改商户的门店
     * @param merchantId
     * @return
     */
    public List<TShop> getByMch(String merchantId) {
        TShopExample tShopExample = new TShopExample();
        tShopExample.createCriteria().andMerchantIdEqualTo(merchantId);
        return tShopMapper.selectByExample(tShopExample);
    }

    /**
     * 获取门店列表
     * @param merchantId
     * @return
     */
    public List<String> getShopIdList(String merchantId) {
        List<TShop> tShopList = this.getByMch(merchantId);
        Set<String> set = new HashSet<>(tShopList.size());
        List<String> shopList = new ArrayList<>(tShopList.size());
        for (TShop tShop : tShopList) {
            set.add(tShop.getShopId());
        }
        shopList.addAll(set);
        return shopList;
    }

}

